*** One-Child Policy, Marriage Distortion, and Welfare Loss ***
*** by Wei Huang, Yinghao Pan and Yi Zhou ***

clear all 
set more off
set scheme s1color
cap log close 
graph set window fontface "Times New Roman"

set niceness 10
gl MAIN = "F:\Dropbox"

gl path1 = "$MAIN/Census"
gl path2 = "$MAIN/Marriage/Working_data"
gl path3 = "$MAIN/Marriage/Restat/dataverse_files/Results" 
gl path4 = "$MAIN/Marriage/Restat/dataverse_files/Graphics" 
gl CONTROL = " year_birth sex#age urban_hk prov_hk##c.(year_birth ) "



use "$path2/unmarried", clear
keep if relation == 0 | relation == 1 | relation == 2 | relation == 6 


merge 1:1 wave area hhid name relation using "$path2/min_marr_policy", keepusing(marry_1 marry_2 marry_3) nogen 



forvalues i = 30(1)35{
gen year = year_birth + `i' 
merge m:1 prov year using "$path2/fines_use", keepusing(fine)

drop if _merge == 2
replace fine = 0 if fine == . & year <= 1979
drop _merge year
ren fine fine_`i'
}



**** Appendix Figure A8(a) ****

preserve 
drop if mi(hktype)

egen n_birth = rowtotal(numbirm numbirf)

keep if sex == 2 & age <= 40
gen nobirth = n_birth == 0 
replace nobirth = nobirth *100
tab age, su(nobirth)
graph bar nobirth, over(age) ytit("Frequency of Females without Children (%)") 
gr export $path4/fertility_dis.pdf, replace

restore 




drop if mi(hktype)
replace urban = hktype == 1 
su age 

cap gen female = 1-male
cap gen han = 1- minority 

replace unmarried = unmarried * 100 
gen unmarr_25 = marr_age > 25 if !mi(marr_age)
replace unmarr_25 = 1 if unmarried == 100 
replace unmarr_25 = unmarr_25 *100 

gen urban_hk = hktype == 2 
gen senior = educ >= 4

egen clsid = group(prov_hk year_birth)


gen marry_9 = unmarried == 0 & (mi(marry_1) & mi(marry_2) & mi( marry_3))

recode marry_1 marry_2 marry_3 (. = 0) 

gen HH = marry_1 * 100 
gen HM = marry_2 * 100 
gen MM = marry_3 * 100 
gen Other = marry_9 * 100 
gen HOMO = HH + MM
su HOMO

keep if age <= 55 
keep if age > 25
keep if year_birth <= 1980 
keep if Other == 0 
tab maritus

keep if sex == 2 // women 

egen n_birth = rowtotal(numbirm numbirf)
replace n_birth = . if age > 45



egen fine_16_20 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20)
egen fine_21_25 = rowmean(fine_21 fine_22 fine_23 fine_24 fine_25)
egen fine_26_30 = rowmean(fine_26 fine_27 fine_28 fine_29 fine_30)
egen fine_21_30 = rowmean(fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 fine_27 fine_28 fine_29 fine_30)
egen fine_16_30 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 fine_27 fine_28 fine_29 fine_30)
egen fine_16_29 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 fine_27 fine_28 fine_29 )
egen fine_16_28 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 fine_27 fine_28  )
egen fine_16_27 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 fine_27 )
egen fine_16_26 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 fine_26 )
*egen fine_16_25 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 fine_25 )
egen fine_16_24 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 fine_24 )
egen fine_16_23 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 fine_23 )
egen fine_16_22 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 fine_22 )
egen fine_16_21 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20 fine_21 )


**** Appendix Table A11 ****

cap erase "$path3/birth_fine_age.xls"
cap erase "$path3/birth_fine_age.txt"

foreach sample in  "n_birth != ."{
foreach gender in "1" {
foreach y_var in "n_birth"  {
foreach policy in "1"   {
foreach eth in  "han" "han == 0"{
foreach age in "16_21" "16_22" "16_23" "16_24" "16_25" "16_26" "16_27"  "16_28" "16_29" "16_30" {
preserve 
keep if `sample' & `policy' & `gender' & `eth'

reghdfe `y_var' fine_`age'   [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/birth_fine_age.xls", append bracket addstat(Y, `r(mean)') ///
 ctit(`sample' &`y_var' & `policy' & `gender' & `eth') keep(fine_`age')  dec(3)
restore


}
}
}
}
}
}


**** Appendix Figure A8(b) ****

import delimited   "$path3/birth_fine_age.txt", varnames(1) stripquote(yes) clear 
drop in 23/30
drop in 1/2
forval i = 3/11{
replace v2 = v`i' if v`i' != ""

}

forval i = 13/21{
replace v12 = v`i' if v`i' != ""

}
replace v1 = "2" if v1 == ""
replace v1 = "1" if v1 != "2"
keep v1 v2 v12
destring v1, replace
gen n = _n
reshape wide v2 v12,i(n) j(v1)
carryforward v21 v121, replace
drop if v22 == ""
ren v21 han
ren v121 mino
ren v22 han_se
ren v122 mino_se
replace han = subinstr(han,"*","",.)
replace han_se = subinstr(han_se,"[","",.)
replace han_se = subinstr(han_se,"]","",.)
replace mino_se = subinstr(mino_se,"[","",.)
replace mino_se = subinstr(mino_se,"]","",.)
destring * ,replace

foreach sample in "han" "mino"{
gen up_`sample' = `sample' + 1.65*`sample'_se
gen low_`sample' = `sample' - 1.65*`sample'_se
}

drop n
gen age = 20+_n
tw (con han mino age, m(O T) lp(longdash longdash) yline(0) xlabel(21(1)30) ///
xtit("Age") ytit("Impact of fines on fertility") ylabel(-0.1(0.05)0.1, grid)) ///
(rarea up_han low_han age, lc(none) lp(dash) color(dkgreen%15)) (rarea up_mino low_mino age, lc(none) lp(dash) color(orange_red%15) ///
legend(label(1 "Han") label(2 "Minority") label(3 "90% CI") label(4 "90% CI") ring(0) pos(12))) 
gr export "$path4\dynamic_fer.pdf", replace 




